struct UF {
    parent :Vec<i32>
   
}

impl UF{
    fn new(n :i32)->UF{
        let mut uf =UF{
            parent:Vec::<i32>::with_capacity(n as usize),
           
        };
        for i in 0..n{
            uf.parent.push(i);
          
        }
        uf
    }
    fn find(&mut self, n :i32)->i32{
        let n =n as usize;
        if self.parent[n]!=n as i32{
            self.parent[n]=self.find(self.parent[n])
        }
        self.parent[n]
    }
    fn union(&mut self,i :i32,j:i32){
        let (x,y)=(self.find(i),j);
        if x==y{
            return;
        }
        self.parent[y as usize]=x
    }
}

impl Solution {
    pub fn find_smallest_set_of_vertices(n: i32, edges: Vec<Vec<i32>>) -> Vec<i32> {
        let mut uf =UF::new(n);
        for i in edges{
            uf.union(i[0], i[1]);
        };
        uf.parent.iter().enumerate().filter(|(ind,val)|{
            *ind==**val as usize
        }).map(|(_,val)|{
            *val
        }).collect()
    }
}